package com.grape.action;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.test.web.ModelAndViewAssert;

import com.grape.pojo.Bookmark_Box;
import com.grape.pojo.User;
import com.grape.service.UserService;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.Validate;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.hibernate.mapping.Value;

@Controller
@Namespace("/")
@Scope("prototype")
@ParentPackage("json-default")
public class UserAction extends BaseAction<User> {
	private static final long serialVersionUID = 1L;

	private File backImg;
	private String backImgFileName;
	private String backImgContentType;

	@Autowired
	private UserService us;

	@Action(value = "user_myTime", results = { @Result(name = "success", type = "json"),
			@Result(name = "notLogin", type = "json") })
	public String myTime() {

		HttpSession session = ServletActionContext.getRequest().getSession();
		User model = (User) session.getAttribute("user");
		User user = null;
		// 用户没有登录,给一个体验用户
		if (model == null) {
			user = us.findByUsername("admin");
		} else {
			user = us.findOne(model.getId());
		}

		HashMap<String, Object> hm = new HashMap<>();
		hm.put("user", user);
		ServletActionContext.getContext().getValueStack().push(hm);
		return SUCCESS;
	}

	// 登录方法,我的哥哥!
	@Action(value = "user_login", results = { @Result(name = "none", type = "json") })
	public String user_login() {

		User user = us.login(model.getUsername(), model.getPassword());
		if (user == null) {
			HashMap<String, Object> hm = new HashMap<>();
			hm.put("error", "用户名或密码错误");
			ServletActionContext.getContext().getValueStack().push(hm);
			return NONE;
		}

		HttpSession session = ServletActionContext.getRequest().getSession();
		session.setMaxInactiveInterval(3600 * 24 * 7);// 周期是7天
		session.setAttribute("user", user);
		// 又想重定向又想传递json少年你想多了
		return NONE;
	}

	// 注册方法
	@Action(value = "user_register", results = {
			@Result(name = "success", type = "redirect", location = "/user/login.html"),
			@Result(name = "register", location = "/user/register.html") })
	public String user_register() {
		us.register(model);
		return SUCCESS;
	}

	// 注册防止用户名重复
	@Action(value = "user_preventRepetitionByUsername", results = { @Result(name = "success", type = "json") })
	public String user_preventRepetitionByUsername() {
		System.err.println(model);
		System.err.println(us);

		User user = us.findByUsername(model.getUsername());
		if (user == null) {
			ServletActionContext.getContext().getValueStack().set("checkResult", "用户名可用");
		} else {
			ServletActionContext.getContext().getValueStack().set("checkResult", "用户名已存在");
		}

		return SUCCESS;
	}

	// 图片上传
	@Action(value = "user_backFile")
	public void user_backFile() throws IOException {

		// 用户上传了背景图片,只需要改session中的user的pictures不需要覆盖保存,记得入库就行了
		User user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
		user = us.findOne(user.getId());

		// 设置绝对路径
		String fileName = System.currentTimeMillis() + backImgFileName + ".png";
		String realPath = ServletActionContext.getServletContext().getRealPath(File.separator + "user") + File.separator
				+ "private_img" + File.separator + user.getUsername() + File.separator + fileName;
		System.out.println("图片存储绝对路径:" + realPath);

		// 复制文件到此路径
		File fff = new File(realPath);
		FileUtils.copyFile(backImg, fff);

		// 获取相对路径,完成上传
		// 前端只要/做分隔符,而File.separator因为在windows用了\,部署到云就好了吧大概
		String contextPath = ServletActionContext.getServletContext().getContextPath() + "/user" + "/private_img" + "/"
				+ user.getUsername() + "/" + fileName;

		user.setPictures(contextPath + "-----" + user.getPictures());
		us.save(user);
	}


	// 保存当前格局
	@Action(value = "user_perfer")
	public void perfer() {
		User user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
		model.setId(user.getId());
		us.setPrefer(model);
	}

	// 删除背景图片
	@Action(value = "user_delBg")
	public void delBg() {
		model = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
		us.delBg(model, bgSrc);
	}

	public void setBackImg(File backImg) {
		this.backImg = backImg;
	}

	public void setBackImgFileName(String backImgFileName) {
		this.backImgFileName = backImgFileName;
	}

	public void setBackImgContentType(String backImgContentType) {
		this.backImgContentType = backImgContentType;
	}
}
